package Model.QBE;

import Model.SQL.*;
import java.util.*;
import java.sql.*;
import Model.Beans.*;
import Model.Beans.Results.*;

public class ProfessorModel extends DataAccessHelperBase {
    
    public ProfessorModel() 
    {
        this.tableName = "professor";
        super.customize(tableName);
    }
    
    public int insert(Person person)
    {
        String[] values = new String[4];
        values[0] = Integer.toString(person.getId());
        values[1] = "'" + person.getName() + "'";
        values[2] = "'" + person.getFirstLastName() + "'";
        values[3] = "'"  + person.getSecondLastName() + "'";
        
        return this.insertRecord(values);
    }
    
    public int delete(String id)
    {
        return this.deleteRecord("professor_id = '" + id + "'");
    }
    
    public int delete(Person person)
    {
        return this.delete(Integer.toString(person.getId()));
    }
    
    public int modify(String id, String name, String firstLastName, String secondLastName)
    {
        String id_str = "professor_id = " + id;
        String[] values = new String[3];
        
        int i = 0;
        if(name.length() > 0)
            values[i++] = "name = '" + name + "'";
        if(firstLastName.length() > 0)
            values[i++] = "first_last_name = '" + firstLastName + "'";
        if(secondLastName.length() > 0)
            values[i++] = "second_last_name = '" + secondLastName + "'";
        return this.updateRecord(id_str, values);   
    }
    
    public int modify(Person person)
    {
        return this.modify(Integer.toString(person.getId()), person.getName(), person.getFirstLastName(), person.getSecondLastName());
    }
    
    public PersonList search(String id, String name, String firstLastName, String secondLastName)
    {
        String where = "";
        ResultSet rs = null;
        PersonList list = new PersonList();
        
        if(id.length() > 0 && !id.equals("0"))
            where += "professor_id = " + id + " AND ";
        if(name.length() > 0)
            where += "name = " + "'" + name + "' AND ";
        if(firstLastName.length() > 0)
            where += "first_last_name = " + "'" + firstLastName + "' AND ";
        if(secondLastName.length() > 0)
            where += "second_last_name = " + "'" + secondLastName + "'";
        else if(where.length() > 0)
            where = where.substring(0, where.length()-4);
        
        
        rs = this.getData(where);
        
        if(rs == null)
            return new PersonList();
        try 
        {
            while(rs.next()) 
            {
                int _professor_id = rs.getInt("professor_id");
                String _name = rs.getString("name");
                String _first_last_name = rs.getString("first_last_name");
                String _second_last_name = rs.getString("second_last_name");
               
                Person tmp = new Person(_professor_id, _name, _first_last_name, _second_last_name);
                
                list.addPerson(tmp);
                //Just for debugging purposes
                System.out.print(_professor_id +" "+ _name + " " + _first_last_name +" " + _second_last_name);
                System.out.println();
            }
            return list;
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: " + e.getMessage());
        }   
        finally
        {
            if (rs != null) 
            {
                try 
                {
                    rs.close();
                } catch (SQLException sqlEx) { }
                rs = null;
            }
        }
        return new PersonList();
    }
    
    public PersonList search(Person person)
    {
        return this.search(Integer.toString(person.getId()), person.getName(), person.getFirstLastName(), person.getSecondLastName());
    }
    
    public Person search_by_id(String id)
    {
        PersonList list = new PersonList();
        Person professor = new Person();
        professor.setId(Integer.parseInt(id));
        
        list = this.search(professor);
        
        if(list.getPersons().size() > 0)
            return (Person)(list.getPersons().get(0));
        else
            return null;
    }
        
    /*
    public static void main(String[] args)
    {
        ProfessorModel pmodel = new ProfessorModel();
        Person p = new Person();
        p.setFistLastName("Toro");
        p.setName("Pepe");
        p.setSecondLastName("shiax");
        p.setId(1);
        //pmodel.insert(p);
        //pmodel.modify(p);
        pmodel.search("", "", "", "");
        pmodel.search(new Person());
        
        p = pmodel.search_by_id("6");
        
        System.out.println(p.toString());
    }
     */
}
